package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.common.util.CheckUtil;
import com.zoo.diannaopeijian.domain.Stock;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.mapper.StockMapper;
import com.zoo.diannaopeijian.service.StockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 库存服务实现类
 */
@Service
public class StockServiceImpl implements StockService {
    @Resource
    private StockMapper stockMapper;

    @Override
    public boolean insertStock(Stock stock) {
        if (CheckUtil.allNullBeanCheck(stock)) {
            return true;
        }
        return stockMapper.insertSelective(stock) == 1;
    }

    @Override
    public boolean increaseStock(Stock stock) {
        if (CheckUtil.allNullBeanCheck(stock)) {
            return true;
        }

        Integer num = stock.getStockNum();

        stock.setStockNum(null);

        List<Stock> oldStocks = stockMapper.select(stock);

        if (oldStocks == null || oldStocks.size() == 0) {
            stock.setStockNum(num);
            return insertStock(stock);
        }

        if (oldStocks.size() != 1) {
            return false;
        }

        return stockMapper.updateStockNum(num, oldStocks.get(0).getStockId()) == 1;
    }


    @Override
    public boolean reduceStock(Stock stock) {
        if (CheckUtil.allNullBeanCheck(stock)) {
            return true;
        }

        Integer num = stock.getStockNum();

        stock.setStockNum(null);

        List<Stock> oldStocks = stockMapper.select(stock);

        if (oldStocks == null || oldStocks.size() == 0) {
            return false;
        }

        if (oldStocks.size() != 1) {
            return false;
        }

        if (oldStocks.get(0).getStockNum() < num) {
            return false;
        }

        return stockMapper.updateStockNum(-num, oldStocks.get(0).getStockId()) == 1;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean reduceStocks(List<Stock> stocks) {
        if (stocks == null || stocks.size() == 0) {
            return true;
        }

        for (Stock stock : stocks) {
            if (!reduceStock(stock)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        }

        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean increaseStocks(List<Stock> stocks) {
        if (stocks == null || stocks.size() == 0) {
            return true;
        }

        for (Stock stock : stocks) {
            if (!increaseStock(stock)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean updateStock(Stock stock) {
        if (CheckUtil.allNullBeanCheck(stock)) {
            return true;
        }

        return stockMapper.updateByPrimaryKeySelective(stock) == 1;
    }

    @Override
    public List<Map> getWareStock(
            Integer componentId,
            List<Integer> componentParameterDetailIds,
            Integer repertoryId,
            Ware ware) {
        return stockMapper.selectWareStock(
                componentId,
                componentParameterDetailIds,
                repertoryId,
                ware);
    }

    @Override
    public List<Map> getWareStockWithPage(
            Page page,
            Integer componentId,
            List<Integer> componentParameterDetailIds,
            Integer repertoryId,
            Ware ware) {
        if (page != null) {
            page.onPage();
        }
        return getWareStock(
                componentId,
                componentParameterDetailIds,
                repertoryId,
                ware);
    }

    @Override
    public List<Map> getFuzzyWareStock(
            Integer componentId,
            List<Integer> componentParameterDetailIds,
            Integer repertoryId,
            Ware ware) {
        return stockMapper.selectFuzzyWareStock(
                componentId,
                componentParameterDetailIds,
                repertoryId,
                ware);
    }

    @Override
    public List<Map> getFuzzyStockWithPage(
            Page page,
            Integer componentId,
            List<Integer> componentParameterDetailIds,
            Integer repertoryId,
            Ware ware) {
        if (page != null) {
            page.onPage();
        }
        return getFuzzyWareStock(
                componentId,
                componentParameterDetailIds,
                repertoryId,
                ware);
    }

    @Override
    public Integer getStockNum(Stock stock) {
        if (CheckUtil.allNullBeanCheck(stock)) {
            throw new NullPointerException("stock 不能为空对象");
        }

        return stockMapper.selectOne(stock).getStockNum();
    }
}
